// pages/login/login.js
Page({

  data:{
    buttonStyle: "",
    checked:false,
    loginStyle: "",
    tel: "",
    code: "",
    codeDisabled: true,
    loginDisabled: true
  },

  isTel(event){
    
    let tel = event.detail.value
    //通过正则表达式判断手机号是否满足
    const res = /^[1][3,4,5,6.7,8,9][0-9]{9}$/
    // console.log(res.test(tel))
    if(!res.test(tel)){
      wx.showToast({
        title: '手机号错误',
        icon: 'error',
        duration: 3000
      })
      this.setData({
        buttonStyle: "",
        codeDisabled: true
      })
    }else{
      //将发送验证码按钮的样式变成红色
      this.setData({
        buttonStyle: "btn",
        codeDisabled:false
      })
    }
  },

  // 更改radio的选中状态
  checkedTap(){
    let tel = this.data.tel
    let code = this.data.code
    this.setData({
      checked:!this.data.checked
    })
    if(this.data.checked && tel !="" && code != ""){
      //选中
      this.setData({
        loginStyle: "loginBtn",
        loginDisabled: false
      })
    }else{
      this.setData({
        loginStyle: "",
        loginDisabled:true
      })
    }
  },

  //获取用户输入的手机号
  getTel(event){
    let tel = event.detail.value
    this.setData({
      tel: tel
    })
    console.log(tel)
  },
  //获取用户输入的验证码
  getCode(event){
    let code = event.detail.value
    this.setData({
      code: code
    })
    // console.log(code)
  },

  // 发送验证码
  sendCode(){
    let tel = this.data.tel
    // console.log("laogui")
    //携带手机号到服务器，去给手机号发送验证码短信
    wx.request({
      
      url: 'http://localhost:9527/user/code/'+tel,
      method: 'GET',
      success: res=>{
        if(res.data.code == 0){
          wx.showToast({
            title: '发送成功',

          })
        }else{
          wx.showToast({
            title: '系统异常',
            icon: "error"
          })
        }
        // console.log(res)
      }
    })
  },

  //点击登录按钮
  login(){
    wx.request({
      url: 'http://localhost:9527/user/login',
      method: "POST",
      data:{
        telphone: this.data.tel,
        code:this.data.code
      },
      success: res=>{
        console.log(res)
        if(res.data.code == 0){
          //先将用户信息进行存储
          wx.setStorageSync('userInfo', res.data.data)
          console.log(res.data.data)
          console.log("userInfo:111")
          wx.navigateBack()
        }
      }
    })
  }

})